<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/12/17
 * Time: 22:21
 */

namespace app\common\model;

use think\Cache;

class YearFortune extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_year_fortune';

    public function getYearFortune($map = [])
    {
        if (empty($map) || !isset($map['rid']) || !isset($map['year']))
        {
            return [];
        }
        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get('fortune_year_' . $map['rid'] . '_' . $map['year']);
        }
        if ($data === false)
        {
            try
            {
                $field = 'fortune,topic,point';
                $data = $this->field($field)->where(['rid' => (int)$map['rid'], 'year' => $map['year']])->findOrFail()->toArray();

                // redis 字符串直接存入redis
//                $data = $data['fortune'];
                $data['scores'] = json_decode($data['fortune'], true);
                $data['point'] = json_decode($data['point'], true);
                unset($data['fortune']);

                Cache::set('fortune_year_' . $map['rid'] . '_' . $map['year'], $data, 86400 * 7);
            }
            catch (\Exception $e)
            {
                $data = [];

//                \think\Log::write('获取年运势失败' . $e->getMessage(), 'error', true);
            }
        }
        return $data;
    }

    public function addFortune($insertData = [])
    {
        $status = true;
        if (empty($insertData))
        {
            return false;
        }
        try
        {
            // 先删除，后添加
            $this->where(['rid' => $insertData['rid'], 'year' => $insertData['year']])->delete();

            $this->insertGetId($insertData);
        }
        catch (\Exception $e)
        {
            $status = false;
            \think\Log::write('年运势写入数据库失败：' . $e->getMessage(), 'error', true);
            \think\Log::write('数据：' . var_export($insertData, true), 'error', true);
        }
        return $status;
    }

    public function getDatas($map = [])
    {
        $result = [
            'scores' => [],
            'topics' => [],
            'topic' => -1,
            'point' => [],
        ];

        if (empty($map))
        {
            return $result;
        }
        $years = $this->getYearFortune($map);
        $result['scores'] = $years['scores'];
        $result['topic'] = $years['topic'];
        $result['point'] = $years['point'];

        $temp = [];
        for ($i = 1; $i < 13; $i++)
        {
            $month = $i < 10 ? '0' . $i : $i;
            $start_time = $map['year'] . '-' . $month;

            $topic = Cache::get('fortune_month_' . $map['rid'] . '_' . $start_time);

            if ($topic == false)
            {
                $temp[] = $start_time;
            } else
            {
                $result['topics'] += json_decode($topic, true);
            }
        }
        if (!empty($temp))
        {
            $where = [
                'rid' => $map['rid'],
                'month' => ['in', $temp]
            ];
            try
            {
                $data = (new MonthFortune())->field('month,fortune')->where($where)->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

            }
            catch (\Exception $e)
            {
                $data = [];
            }

            if (!empty($data))
            {
                foreach ($data as $d)
                {
                    Cache::set('fortune_month_' . $map['rid'] . '_' . $d['month'], $d['fortune'], 86400 * 7);
                    $result['topics'] += json_decode($d['fortune'], true);
                }
            }
        }
        return $result;
    }
}
